<?php

abstract class MController extends CController
{
	public $util;
	protected $request_method = 'param';
	
	public function __construct($id, $module = null)
	{
		parent::__construct($id, $module);
		$this->util =& Yii::app()->util;
	}
	
	/**
	 * 实现CActiveRecord::model()方法，5.3也以下可用
	 * @param string $name
	 * @param string $table
	 * @param mixed $pk
	 * @return DBModel
	 */
	public function model($name, $table = null, $pk = null)
	{
		$name = ucfirst($name);
		if (is_file(PROTECTED_PATH . '/models/' . $name . 'Model.php')) {
			return call_user_func(array($name . 'Model', 'model'), $name);
		}
		return new ModelObject($table, $pk);
	}
	
	public function getDbConnection($t, $idx = false)
	{
		return MCDbConnection::getDbConnection($t, $idx);
	}
	
	public function return_json($code, $msg)
	{
		echo json_encode(array(
			'code' => $code,
			'msg' => $msg
		));
		die;
	}
	
	public function getRequest($key, $defaultValue = null)
	{
		switch ($this->request_method) {
			case 'get':
				return Yii::app()->request->getQuery($key, $defaultValue);
				break;
			case 'post':
				return Yii::app()->request->getPost($key, $defaultValue);
				break;
			case 'param':
				return Yii::app()->request->getParam($key, $defaultValue);
				break;
		}
	}
}